package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.xml.ws.AsyncHandler;

import com.ejia.dao.DBconnection;
import com.ejia.pojo.house;
import com.ejia.pojo.user;
import com.ejia.server.HouseService;
import com.ejia.server.UserService;
import com.ejia.dao.BaseDao;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.HeadlessException;

import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class zc extends JFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private JPasswordField txtPwd;
	private JTextField textField;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					zc frame = new zc();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public zc() {
		setFont(new Font("Dialog", Font.PLAIN, 30));
		setTitle("用户注册页面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 540, 643);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("账号");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel.setBounds(56, 161, 81, 61);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("密码");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_1.setBounds(56, 236, 81, 61);
		contentPane.add(lblNewLabel_1);

		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 30));
		txtName.setBounds(193, 176, 269, 32);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 30));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"丽景豪庭", "时代名都", "黄金国际", "中瑞华侨城"}));
		comboBox.setBounds(193, 398, 269, 34);
		contentPane.add(comboBox);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 30));
		textField.setBounds(193, 330, 269, 32);
		contentPane.add(textField);
		textField.setColumns(10);

		txtPwd = new JPasswordField();
		txtPwd.setFont(new Font("宋体", Font.PLAIN, 30));
		txtPwd.setBounds(193, 251, 269, 32);
		contentPane.add(txtPwd);

		JButton btnNewButton = new JButton("注册");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = txtName.getText();
				char[] passwrod = txtPwd.getPassword();
				String pwd = new String(passwrod);
				String realname= textField.getText();
				int parentid=0;
				if (comboBox.getSelectedItem().equals("丽景豪庭")) {
					parentid=1;
				}else if (comboBox.getSelectedItem().equals("时代名都")) {
					parentid=2;
				}else if (comboBox.getSelectedItem().equals("黄金国际")) {
					parentid=3;
				}else if (comboBox.getSelectedItem().equals("中瑞华侨城")) {
					parentid=4;
				}
				String mph=textField_2.getText();
				if (name.equals("") || pwd.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入账号密码");
				} else {
					DBconnection.getconn();
					String sql = "select * from tb_user where uid=?";
					try {
						if (BaseDao.query(sql, name).next()) {
							JOptionPane.showMessageDialog(null, "用户已存在");

						} else {
							String sql2="insert into tb_house(hname,parentid) values(?,?)";
							sql = "insert into tb_user(uid,pwd,realname,hid) values(?,?,?,?)";
							String sql1="insert into tb_car(carno,carnumber,userid,statu) values(\" \",\" \",?,\" \")";
							if (BaseDao.execute(sql2,mph,parentid)) {
								if (BaseDao.execute(sql, name, pwd,realname,HouseService.hid(mph, parentid))) {
									ArrayList<user> users= new ArrayList<>();
									users=UserService.findRealname(name);
									int userid=users.get(0).getId();
									if (BaseDao.execute(sql1, userid)) {
										JOptionPane.showMessageDialog(null, "注册成功");
										dispose();
									}else {
										JOptionPane.showMessageDialog(null, "注册失败");
									}
								} else {
									JOptionPane.showMessageDialog(null, "注册失败");
								}
							}else {
								JOptionPane.showMessageDialog(null, "注册失败");
							}
							

						}
					} catch (HeadlessException | SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}

			}
		});

		JLabel label = new JLabel("注册");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("宋体", Font.PLAIN, 62));
		label.setBounds(110, 27, 298, 117);
		contentPane.add(label);
		
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 30));
		textField_2.setColumns(10);
		textField_2.setBounds(193, 460, 269, 32);
		contentPane.add(textField_2);
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 24));
		btnNewButton.setBounds(93, 512, 120, 60);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 24));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnNewButton_1.setBounds(305, 512, 120, 60);
		contentPane.add(btnNewButton_1);
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 30));
		label_1.setBounds(56, 325, 81, 42);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("小区");
		label_2.setFont(new Font("宋体", Font.PLAIN, 30));
		label_2.setBounds(56, 398, 101, 32);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("门牌号");
		label_3.setFont(new Font("宋体", Font.PLAIN, 30));
		label_3.setBounds(56, 450, 101, 42);
		contentPane.add(label_3);
		
		
		setLocationRelativeTo(null);
	}
}
